package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.mine.entity.OrderRefundCancelBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderRefundStateDataBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 查看退货状态网络请求层
 * 包括请求退货状态和取消订单两个网络请求
 */
public class OrderAfterSaleStateModel implements OrderAfterSaleStateModelImpl{

    private OrderAfterSaleStateModel.OrderAfterSaleStateDataResponseListener mListener;

    private OrderAfterSaleStateModel.OrderCancelRefundResponseListener mCancelListener;


    private static final String TAG = "OrderAfterSaleStateModel";

    public OrderAfterSaleStateModel(OrderAfterSaleStateModel.OrderAfterSaleStateDataResponseListener listener,
                                    OrderAfterSaleStateModel.OrderCancelRefundResponseListener cancelListener) {
        mListener = listener;
        mCancelListener=cancelListener;
    }
    public interface OrderAfterSaleStateDataResponseListener{
        void onGetDetailSuccess(OrderRefundStateDataBean bean);
        void onGetDetailError(Throwable throwable);
    }

    /**
     * 取消请求监听事件
     */
    public interface OrderCancelRefundResponseListener{
        void onGetCancelDetailSuccess(OrderRefundCancelBean bean);
        void onGetCancelDetailError(Throwable throwable);
    }
    /**
     * 请求退货状态信息数据
     * @param mOrdersId 订单id
     */
    @Override
    public void getOrderAfterSaleState(String mOrdersId) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("orders_id", mOrdersId)
                .build();
        io.reactivex.Observable<OrderRefundStateDataBean> observable = DataManager.getOrderRefundStateData(body);
        io.reactivex.Observable<OrderRefundStateDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderRefundStateDataBean>() {
            @Override
            public void onNext(OrderRefundStateDataBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }

    /**
     * 取消退款网络请求
     * @param mOrdersId 订单id
     */
    @Override
    public void getOrderCancelRefundCode(String mOrdersId) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("orders_id", mOrdersId)
                .build();
        io.reactivex.Observable<OrderRefundCancelBean> observable = DataManager.getOrderRefundCancelData(body);
        io.reactivex.Observable<OrderRefundCancelBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderRefundCancelBean>() {
            @Override
            public void onNext(OrderRefundCancelBean bean) {
                if (mCancelListener != null) {
                    mCancelListener.onGetCancelDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mCancelListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mCancelListener.onGetCancelDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }
}
